﻿#nullable enable

using System;
using PHMEE.CommonUtils;
using PHMEE.Localization;

namespace PHMEE.EnergyEfficiency;

public class LogAbstractCommonAppService : PHMEEAppService
{
    private readonly IPHMEETimeProvider _phmeeTimeProvider;

    public LogAbstractCommonAppService(IPHMEETimeProvider phmeeTimeProvider)
    {
        LocalizationResource = typeof(EnergyEfficiencyResource);
        
        _phmeeTimeProvider = phmeeTimeProvider;
    }

    /// <summary>
    /// 构建 LogAbstract - 所有船型
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="input"></param>
    /// <returns></returns>
    internal LogAbstract BuildLogAbstract(Guid vesselId, LogAbstractDto input)
    {
        // 将 TimeUTC 精确到分钟
        var logDateTimeUtc = DateTimeUtils.GetUtcDateTimeByDateOnlyTimeOnlyUpToMinute(input.DateUTC, input.TimeUTC);

        var logAbstract = ObjectMapper.Map<LogAbstractDto, LogAbstract>(input);
        
        // 不使用 input 的 Ts，input 的 Ts 仅用于更新时作为 primary key 删除旧数据，新数据的 Ts 由 DateUTC 和 TimeUTC 生成
        logAbstract.Ts = logDateTimeUtc;
        var utcNow = _phmeeTimeProvider.GetUtcNow();
        logAbstract.CreationTime = utcNow;
        logAbstract.LastModifiedTime = utcNow;
        logAbstract.Id = vesselId;
        
        return logAbstract; 
    }
}